package net.wenku8.reader.ui.lite

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import androidx.compose.foundation.background
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.ClickableText
import androidx.compose.material3.Checkbox
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FilterChip
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.Snackbar
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldColors
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import net.wenku8.reader.ui.theme.LiteBlue
import net.wenku8.reader.ui.theme.LiteGray
import androidx.core.net.toUri

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun LiteTextField(
    value: String,
    onValueChange: (String) -> Unit,
    visualTransformation: VisualTransformation = VisualTransformation.None,
    placeholder: @Composable (() -> Unit)? = null,
    leadingIcon: @Composable (() -> Unit)? = null,
    trailingIcon: @Composable (() -> Unit)? = null,
    @SuppressLint("ModifierParameter") modifier: Modifier = Modifier,
    colors: TextFieldColors = TextFieldDefaults.colors(),
    maxLines: Int = 1,
) {
    BasicTextField(
        value = value,
        onValueChange = onValueChange,
        modifier = modifier,
        maxLines = maxLines,
        textStyle = LocalTextStyle.current.merge(fontSize = 15.sp),
        visualTransformation = visualTransformation,
        decorationBox = @Composable { innerTextField ->
            TextFieldDefaults.DecorationBox(
                value = value,
                visualTransformation = visualTransformation,
                innerTextField = innerTextField,
                placeholder = placeholder,
                leadingIcon = leadingIcon,
                trailingIcon = trailingIcon,
                singleLine = true,
                enabled = true,
                interactionSource = remember { MutableInteractionSource() },
                colors = colors,
                contentPadding = PaddingValues(top = 1.dp, bottom = 1.dp, start = 10.dp, end = 10.dp)
            )
        }

    )
}

@Composable
fun LiteTitle(text: String, fontSize: TextUnit = 17.sp, color: Color = Color.Black, maxLines : Int = 1) {
    Text(text = text, color = color, fontSize = fontSize, maxLines = maxLines, fontWeight = FontWeight.Bold,
        overflow = TextOverflow.Ellipsis, modifier = Modifier.smallPadding())
}

@Composable
fun LiteLabel(text: String, fontSize: TextUnit = 15.sp, color: Color = Color.Black) {
    Text(text = text, color = color, fontSize = fontSize, maxLines = 1, fontWeight = FontWeight.Bold,
        overflow = TextOverflow.Ellipsis, modifier = Modifier.smallPadding())
}

@Composable
fun LiteText(text: String, fontSize: TextUnit = 15.sp, color: Color = Color.Black, showAll : Boolean = false, maxLines : Int = 1, indent : Int = 1) {
    Text(text = text, color = color, fontSize = fontSize,
        maxLines = if (!showAll) maxLines else Int.MAX_VALUE,
        overflow = TextOverflow.Ellipsis,
        modifier = Modifier.smallPadding().padding(start = (indent).dp))
}


@Composable
fun LiteCheckBox(text : String, data : MutableState<Boolean>){
    LiteLine{
        Checkbox(
            checked = data.value,
            modifier = Modifier.height(32.dp),
            onCheckedChange = { data.value = it }
        )
        LiteText(text)
    }
}


@Composable
fun LiteSwitch(optionMap : Map<String, String>, data : MutableState<String>, onSelected : (String) -> Unit = {}){
    LiteLine{
        optionMap.forEach{ entry ->
            FilterChip(
                modifier = Modifier.padding(end = 5.dp)
                    .background(color = if (entry.key == data.value) Color.LightGray else LiteGray),
                onClick = {
                    data.value = entry.key
                    onSelected(entry.key)
                },
                label = { LiteText(entry.value) },
                selected = entry.key == data.value,
            )
        }
    }
}





@Composable
fun LiteWebLink(url: String, text: String) {
    val annotatedString = buildAnnotatedString {
        append(text)
        addStyle(
            style = SpanStyle(color = LiteBlue, fontSize = 15.sp),
            start = 0,
            end = text.length
        )
        addStringAnnotation(
            tag = "URL",
            annotation = url,
            start = 0,
            end = text.length
        )
    }
    val context = LocalContext.current
    ClickableText(
        text = annotatedString,
        onClick = { offset ->
            annotatedString.getStringAnnotations(tag = "URL", start = offset, end = offset)
                .firstOrNull()?.let { annotation ->
                    val intent = Intent(Intent.ACTION_VIEW, annotation.item.toUri())
                    context.startActivity(intent)
                }
        },
        modifier = Modifier.smallPadding()
    )
}



@Composable
fun LiteLoading(text: String) {
    Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ){
        Row {
            CircularProgressIndicator(
                modifier = Modifier.width(18.dp).padding(top = 3.dp),
                color = Color.Black
            )
            Column(
                modifier = Modifier.padding(start = 10.dp)
            ) {
                LiteText(text = text)
            }
        }
    }
}


@Composable
fun LiteSnackbarHost(hostState: SnackbarHostState){
    SnackbarHost(
        hostState = hostState,
    ){
        Snackbar(
            snackbarData = it,
            containerColor = Color.LightGray,
            contentColor = Color.Black,
            actionOnNewLine = true
        )
    }
}